package com.he.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Department;
import com.he.pojo.Role;
import com.he.pojo.RoleExample;
import com.he.pojo.User;
import com.he.pojo.UserExample;
import com.he.pojo.userbean;
import com.he.service.department.DepartmentServiceI;
import com.he.service.role.RoleServiceI;
import com.he.service.user.UserServiceI;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	UserServiceI userServiceI;
	@Autowired
	RoleServiceI roleServicel;
	@Autowired
	DepartmentServiceI departServiceI;
	@RequestMapping("/show")
	public String userShow(User user,ModelMap map) {
		//分页参数
		StringBuffer params=new StringBuffer();
		UserExample example = new UserExample();
		if(StringUtils.isNotBlank(user.getUserName())) {
			example.createCriteria().andUserNameLike("%"+user.getUserName()+"%");
			params.append("&userName="+user.getUserName());
		}
		List<User> users = userServiceI.getUsers(example, user);
		for (User user2 : users) {
			user2.setRole(roleServicel.getRoleById(user2.getRoleId()).getRoleName());
			user2.setDepart(departServiceI.getDepartmentById(user2.getDepartmentId()).getDeptName());
		}
		//为前端传递查询结果list
		map.addAttribute("users", users);
		/*为user总页数赋值,向前端传总页数*/
		PageInfo<User> info=new PageInfo<>(users);
		//为user总页数赋值
		user.setPages(info.getPages());
		//为user总记录数赋值
		user.setTotal(info.getTotal());
		/*查询条件*/
		user.setParams(params.toString());
		/*为role返回的controller链接赋值*/
		user.setUrl("show.do");
		/*向前端传参数保存在user中*/
		map.addAttribute("user", user);
		//转发地址要根据页面修改
		return "user";

	}
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public String userUpdate(User user,ModelMap map) {
		//从前端获取id，使用id查找
		User user2 = userServiceI.getUserById(user.getId());
		Role role=new Role();
		role.setWithPage(0);
		List<Role> rlist = roleServicel.getRoles(null, role);
		Department department=new Department();
		department.setWithPage(0);
		List<Department> dlist = departServiceI.getDepartments(null, department);
		map.addAttribute("user",user2);
		map.addAttribute("rlist", rlist);
		map.addAttribute("dlist", dlist);
		//转发到修改表单页
		return "edituser";
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String user(User user,ModelMap map) {
		//从前端获取表单数据
		int user2 = userServiceI.update(user);
		map.addAttribute("user",user2);
		return "redirect:show.do";
	}
	
	@RequestMapping(value={"/add"},method=RequestMethod.POST)
	public String userAdd(userbean ub) {
		User user=new User();
		user.setWithPage(0);
		Role role=new Role();
		role.setWithPage(0);
		user.setUserAccount(ub.getUserAccount());
		user.setUserPwd(ub.getUserPwd());
		user.setUserName(ub.getUserName());
		RoleExample roleExample=new RoleExample();
		com.he.pojo.RoleExample.Criteria cc = roleExample.createCriteria();
		cc.andRoleNameEqualTo(ub.getRoleName());
		List<Role> rlist= roleServicel.getRoles(roleExample, role);
		user.setRoleId( rlist.get(0).getId());
		user.setDepartmentId(rlist.get(0).getDeptId());
		user.setUserTel(ub.getUserTel());
	    user.setUserState(ub.getUserState());
		userServiceI.insert(user);
		return "redirect:show.do";
		
	}
	@RequestMapping("/addshow")
	public String userAddshow(HttpServletRequest request) {
		Role role=new Role();
		role.setWithPage(0);
		List<Role> rlist=new ArrayList<>();
		rlist=roleServicel.getRoles(null, role);
		request.setAttribute("rlist", rlist);
		List<Department> dlist=new ArrayList<>();
		Department depart=new Department();
		dlist=departServiceI.getDepartments(null, depart);
		request.setAttribute("dlist", dlist);
		return "adduser";
		
	}

	@RequestMapping("/delete")
	public String fun(HttpServletRequest request) {
		String str=request.getParameter("id");
		int id=Integer.parseInt(str);
		int r= userServiceI.delete(id);
		return "redirect:show.do";
	}
	}
